package com.yicong.ants.activity;

import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity;
import com.cchao.simplelib.ui.adapter.StatefulBindQuickAdapter;
import com.kennyc.view.MultiStateView;
import com.yicong.ants.BR;
import com.yicong.ants.R;
import com.yicong.ants.bean.DoubleConfirmItem;
import com.yicong.ants.bean.base.ListRespBean;
import com.yicong.ants.bean.base.RespBean;
import com.yicong.ants.databinding.RecyclerBinding;
import com.yicong.ants.databinding.RecyclerRefreshBinding;
import com.yicong.ants.manager.http.RetrofitHelper;
import com.yicong.ants.view.dialog.GiftConfirmationDialog;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;


public class DoubleConfirmActivity extends BaseTitleBarActivity<RecyclerRefreshBinding> implements View.OnClickListener {

    @Override
    protected int getLayout() {
        return R.layout.recycler_refresh;
    }

    private StatefulBindQuickAdapter<DoubleConfirmItem> mAdapter;
    private GiftConfirmationDialog dialog;
    //0退回 1确认
    private int status;
    private int position;

    @Override
    protected void initEventAndData() {
        status = getIntent().getIntExtra("status", 0);
        setTitleText("二次确认");
        initAdapter();
        onLoadData();
    }

    private void initAdapter() {
        mDataBind.recycler.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new StatefulBindQuickAdapter<DoubleConfirmItem>(R.layout.double_confirm, null) {
            @Override
            public void loadPageData(int page) {
                loadPage(page);
            }

            @Override
            protected void convert(DataBindViewHolder helper, DoubleConfirmItem item) {
                TextView textView = helper.getView(R.id.control_btn);
                if (status == 0) {
                    textView.setText("退回");
                } else {
                    textView.setText("确认");
                }
                helper.addOnClickListener(R.id.control_btn);
                helper.getBinding().setVariable(BR.item, item);
            }
        };

        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (view.getId() == R.id.control_btn) {
                if (dialog == null) {
                    dialog = new GiftConfirmationDialog(this, status);
                    dialog.setOnClickListener(this);
                }
                this.position = position;
                dialog.show(getSupportFragmentManager());
            }
        });
        mAdapter.bindToRecyclerView(mDataBind.recycler);

        mDataBind.swipeRefresh.setOnRefreshListener(() -> loadPage(1));

    }

    @Override
    protected void onLoadData() {
        loadPage(1);
    }

    private void loadPage(int page) {
        showProgress();
        Map<String, String> map = new HashMap<>();
        map.put("page_size", "20");
        map.put("page", String.valueOf(page));
        if (page == 1) {
            mAdapter.setViewState(MultiStateView.VIEW_STATE_LOADING);
        }
        Observable<ListRespBean<DoubleConfirmItem>> api;
        if (status == 0) {
            api = RetrofitHelper.getApis().getTransferConfirm(map);
        } else {
            api = RetrofitHelper.getApis().toTransferConfirm(map);
        }
        addSubscribe(api.compose(RxHelper.toMain())
            .subscribe(respBean -> {
                hideProgress();
                mDataBind.swipeRefresh.setRefreshing(false);
                if (respBean.isCodeFail()) {
                    showToast(respBean.getMsg());
                    return;
                }
                mAdapter.solveData(respBean.getData(), page, 20);
            }, RxHelper.getHideProgressConsumer(this)));
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.cancel) {
            dialog.dismiss();
        } else if (id == R.id.confirm) {
            dialog.dismiss();
            op();
        }
    }

    private void op() {
        Map<String, String> map = new HashMap<>();
        map.put("transfer_id", mAdapter.getData().get(position).getTransfer_id());
        Observable<RespBean> api;
        if (status == 0) {
            api = RetrofitHelper.getApis().transferBack(map);
        } else {
            api = RetrofitHelper.getApis().transferConfirm(map);
        }
        addSubscribe(api.compose(RxHelper.toMain())
            .subscribe(respBean -> {
                hideProgress();
                if (respBean.isCodeFail()) {
                    showToast(respBean.getMsg());
//                    return;
                }
//                mAdapter.remove(position);
                loadPage(1);
//                if (mAdapter.getData().size() == 0) {
//                    mAdapter.setViewState(MultiStateView.VIEW_STATE_EMPTY);
//                }
            }, RxHelper.getHideProgressConsumer(this)));
    }
}